<?php
/**
 * Created by 汉国软件.
 * User: 舒中荣
 * Date: 2019-09-27
 * Time: 16:14
 */

namespace App\Http\Controllers\Users;

use App\Api\Helpers\Api\ApiResponse;
use App\Models\Admin;
use App\Models\BeforeMeeting;
use App\Models\MeetingIssue;
use App\Models\MeetingIssuesModel;
use App\Models\MeetingPower;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class NewApiController extends BaseController
{
    use ApiResponse;
    protected $user_id = 0;
    protected $token = '';
    protected $user_code = 0;


    public function __construct(Request $request)
    {
        $this->token = $request->header('token');
        if ($this->token) {
            $user = Admin::where('token', $this->token)->first();
            if ($user) {
                $this->user_id = $user->id;
                $this->user_code = $user->account;
            }

        }
    }

    /**
     * 议题列表
     * @param Request $request
     * @return mixed
     */
    public function meetingIssue(Request $request)
    {
        $input = $request->only('meeting_id');
        $result = Validator::make($input, [
            'meeting_id' => 'required|integer'
        ], [
            'meeting_id.required' => '缺少会议参数！',
            'meeting_id.integer' => '会议参数错误！',
        ]);
        if ($result->fails()) {
            return $this->setStatusCode(412)->message($result->errors()->first());
        }
        if (!BeforeMeeting::checkWhere([['id', $input['meeting_id']], ['status', '0'], ['time_start', '>', date('Y-m-d H:i:s')]])) {
            return $this->setStatusCode(412)->message('此会议不存在或已在进行中！');
        }
        if (!MeetingPower::check(['meeting_id' => $input['meeting_id'], 'staff_id' => $this->user_id])) {
            return $this->setStatusCode(412)->message('你未参加此会议，不可提交议题！');
        }
        $list = MeetingIssue::getWeList(['meeting_id' => $input['meeting_id'], 'status' => 0]);
        return $this->success(['list' => $list]);
    }

    /**
     * 增加议题
     * @param Request $request
     * @return mixed
     */
    public function submitIssue(Request $request)
    {
        $input = $request->only('issue_id', 'file');
        $result = Validator::make($input, [
            'issue_id' => 'required|integer',
            'file' => 'required|array',
        ], [
            'issue_id.required' => '缺少议题参数！',
            'issue_id.integer' => '议题参数错误！',
            'file.required' => '缺少议题文件参数！',
            'file.array' => '文件参数错误！',
        ]);
        if ($result->fails()) {
            return $this->setStatusCode(412)->message($result->errors()->first());
        }
        if (empty($input['file'])) {
            return $this->setStatusCode(412)->message('文件参数错误！');
        } else {
            foreach ($input['file'] as $value) {
                if (!array_key_exists('name', $value) || !array_key_exists('url', $value)) {
                    return $this->setStatusCode(412)->message('会议文件格式错误！');
                }
            }
        }
        $data = MeetingIssue::getFirst($input['issue_id']);
        if (!BeforeMeeting::checkWhere([['id', $data['meeting_id']], ['status', '0'], ['time_start', '>', date('Y-m-d H:i:s')]])) {
            return $this->setStatusCode(412)->message('此会议不存在或已在进行中！');
        }
        if (!MeetingPower::check(['meeting_id' => $data['meeting_id'], 'staff_id' => $this->user_id])) {
            return $this->setStatusCode(412)->message('你未参加此会议，不可提交议题！');
        }
        $input['staff_id'] = $this->user_id;
        if (MeetingIssue::submitIssue($input)) {
            return $this->message();
        } else {
            return $this->failed();
        }
    }

    /**
     * 议题详情
     * @param Request $request
     * @return mixed
     */
    public function IssueDetail(Request $request)
    {
        $input = $request->only('issue_id');
        $result = Validator::make($input, [
            'issue_id' => 'required|integer',
        ], [
            'issue_id.required' => '缺少议题参数！',
            'issue_id.integer' => '议题参数错误！',
        ]);
        if ($result->fails()) {
            return $this->setStatusCode(412)->message($result->errors()->first());
        }
        $data = MeetingIssue::issueInfo($input['issue_id']);
        return $this->success($data);
    }
}